package com.changwu.cwbase.base;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.changwu.cwbase.App;
import com.changwu.cwbase.R;
import com.changwu.cwbase.net.RxManager;
import com.jaeger.library.StatusBarUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;


/**
 * 基类Activity
 */
public abstract class ActivityBase extends AppCompatActivity {
    protected Unbinder mBinder;
    protected ActivityBase activity;
    protected Toolbar toolbar;
    protected TextView title;
    protected TextView editor;
    protected ImageView ivEditor;
    protected String TAG;

    @Override
    public void setContentView(int layoutResId) {
        super.setContentView(layoutResId);
        //Butter Knife初始化
        mBinder = ButterKnife.bind(this);
        initToolBar();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        StatusBarUtil.setColor(this,
                getResources().getColor(R.color.color_white_trans), 10);
        activity = this;
        TAG = getPackageName() + "." + getClass().getSimpleName();
        App.getInstance().addActivity(this);
    }

    /**
     * 初始化标题栏
     */
    protected void initToolBar() {
        if (toolbar == null) {
            toolbar = (Toolbar) findViewById(R.id.header);
        }
        if (toolbar != null) {
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            title = findViewById(R.id.title);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBinder != null) {
            mBinder.unbind();
        }
        RxManager.getInstance().clear(TAG);
        App.getInstance().finishActivity(this);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN &&
                getCurrentFocus() != null &&
                getCurrentFocus().getWindowToken() != null) {

            View v = getCurrentFocus();
            if (isShouldHideKeyboard(v, event)) {
                hideKeyboard(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(event);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            Rect rectangle = new Rect();
            getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
            //高度为rectangle.top-0仍为rectangle.top
            int[] l = {0, 0};
            v.getLocationOnScreen(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            //Toast.makeText(activity,event.getRawX()+"-"+left+"--"+ event.getRawX()+"-"+right+"--"+(event.getRawY()-rectangle.top)+"-"+top+"--"+(event.getRawY()-rectangle.top)+"-"+bottom,Toast.LENGTH_LONG).show();
            if (event.getRawX() > left && event.getRawX() < right
                    && (event.getRawY() - rectangle.top) > top && (event.getRawY() - rectangle.top) < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager mInputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            mInputMethodManager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    protected void setRightTxt(String content, View.OnClickListener listener) {
        editor = findViewById(R.id.editor);
        if (editor != null) {
            editor.setVisibility(View.VISIBLE);
            editor.setText(content);
            editor.setOnClickListener(listener);
        }
    }

    protected void setRightImage(int ico, View.OnClickListener listener) {
        ivEditor = findViewById(R.id.ivEditor);
        if (editor != null) {
            ivEditor.setVisibility(View.VISIBLE);
            ivEditor.setBackgroundResource(ico);
            ivEditor.setOnClickListener(listener);
        }
    }
}
